package com.practice.controller;
import com.practice.entity.Department;
import com.practice.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
 * @program: tenghw-practice-demo
 * @author: tenghw
 * @create: 2024-01-19 01:37
 * @Version 1.0
 * @description:
 **/
@Slf4j
@RestController
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    @GetMapping( "/recursionDepartment")
    public String recursionDepartment(@RequestParam(value = "method",required = true)  Integer method,
                                      @RequestParam(value = "id",required = false)  Integer id){
        List<Department> list=null;
        String jsonResult="";
        try {
            list = departmentService.findAll();
            if(method == 1){
                jsonResult= departmentService.recursionDepartment1(list,id);
            }else{
                jsonResult= departmentService.recursionDepartment2(list,id);
            }
            return jsonResult;
        }catch (Exception e){
            e.printStackTrace();
            return e.toString();
        }
    }

    // 优化：在sql层做递归查询
    @GetMapping( "/recursionDepartment1")
    public String recursionDepartment1(@RequestParam(value = "method",required = true)  Integer method,
                                       @RequestParam(value = "id",required = false)  Integer id){
        List<Department> list=null;
        String jsonResult="";
        try {
            if(id != null){
                list= departmentService.getDepartmentById(id);
            }else{
                list = departmentService.findAll();
            }
            if(method == 1){
                jsonResult= departmentService.recursionDepartment1(list,id);
            }else{
                jsonResult= departmentService.recursionDepartment2(list,id);
            }
            return jsonResult;
        }catch (Exception e){
            e.printStackTrace();
            return e.toString();
        }
    }
}